<template>
	<!-- 	<page-meta>
		<navigation-bar :title="nbTitle" :subtitle-color="nbFrontColor" :loading="nbLoading" :front-color="nbFrontColor"
			:background-color="nbBackgroundColor" />
	</page-meta> -->
	<view>

		<view v-for="(item,index) in list" :key="index">
			<uni-card style="height: 400rpx; position: relative;" @click="onLongPress(item.msgId)">

				<view class="msgTitle">
					{{item.msgTitle}}
				</view>
				<view class="time">
					{{item.createTime}}
				</view>
				<mp-html class="msgContent" :content="item.msgContent" />
				<view class="remark">
					班主任:{{item.remark}}
				</view>
				<view class="footerTop" @click.stop="gotoDetails(item)">
					<view class="" style="margin-top: 10rpx;">
						<text class="information">查看详情</text>
						<text class="iconfont">&#xe64c;</text>
					</view>

				</view>
			</uni-card>
			<view class="bottomTime">
				{{item.formatDate}}
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getHomeWorkClass
	} from "@/api/class/class.js"
	import {
		formatDate
	} from '@/api/time.js';
	import {
		getPageClassHomeWork,
		deleteMyMessage,
		updateTopping
	} from "@/api/message/message.js"
	export default {
		data() {
			return {
				nbTitle: '家庭作业',
				nbLoading: false,
				nbFrontColor: '#000000',
				nbBackgroundColor: '#fcc10d',
				homeWorkList: [],
				list: [],
				classId: '',
				total: null, //总条数
				data: {
					pageNum: 1, //第几页
					pageSize: 4, //显示多少条
				}
			};
		},

		onLoad(option) {
			const classId = JSON.parse(decodeURIComponent(option.classId));
			this.classId = classId
			this.getPageClassHomeWork()
		},
		methods: {
			getPageClassHomeWork() {

				getPageClassHomeWork(this.classId, this.data).then((res) => {

					// if (res.rows) {
					// 	this.homeWorkList = [...this.homeWorkList, ...res.rows]
					// }
					this.homeWorkList = res.rows
					this.total = res.total
					for (let i = 0; i < this.homeWorkList.length; i++) {
						this.$set(this.homeWorkList[i], "formatDate", "")
						this.homeWorkList[i].formatDate = formatDate(Date.parse(this.homeWorkList[i].createTime))
						this.homeWorkList[i].createTime = this.happenTimeFun(this.homeWorkList[i].createTime)
						this.list.push(this.homeWorkList[i])

					}

				})
			},
			happenTimeFun(time) { //时间戳数据处理
				let arr = [];
				arr = time.split("-");
				let month = "";
				for (let i = 0; i < arr[1].length; i++) {
					if (i == 0) {
						if (arr[1][i] != "0") {
							month += arr[1][i];
						}
					} else {
						month += arr[1][i];
					}
				}
				month += "月"
				let oldDay = arr[2].split(" ")[0];
				let newDay = "";
				for (let i = 0; i < oldDay.length; i++) {
					if (i == 0) {
						if (oldDay[i] != "0") {
							newDay += oldDay[i];
						}
					} else {
						newDay += oldDay[i];
					}

				}
				newDay += "日"
				return arr[0] + "年" + month + newDay
			},
			gotoDetails(event) {
				let that = this
				getHomeWorkClass(event.createBy).then((res) => {

					that.$set(event, "className", res)
				}).then()

				setTimeout(() => {
					updateTopping(event.msgId, 3).then((res) => {
						uni.navigateTo({
							url: '/pages/home/homeWork-details/index?item=' +
								encodeURIComponent(
									JSON
									.stringify(
										event))
						})

					})

				}, 50)
			},
			onReachBottom() { //触底事件
				if (this.data.pageNum * this.data.pageSize >= this.total) {
					uni.showToast({
						title: '没有更多数据了',
						icon: 'none',
						duration: 1000
					});
					setTimeout(() => {
						uni.hideLoading()
					}, 500)
				} else {
					if (this.data.pageNum <= this.data.pageNum - 1) {
						setTimeout(() => {
							uni.hideLoading()
						}, 500)
					} else {
						uni.showLoading({
							title: '加载中'
						});
						this.data.pageNum++

						this.getPageClassHomeWork()
					}
					setTimeout(() => {
						uni.hideLoading()
					}, 500)
				}
			},
			// 长按删除
			onLongPress(e) {
				let that = this
				uni.showModal({ // 弹框询问是否进行下一步事件
					title: '提示',
					content: '是否删除作业消息',
					success: function(res) {
						if (res.confirm) {
							var NewList = that.list

							deleteMyMessage(e).then((res) => {
								that.list = []
								for (let i = 0; i < NewList.length; i++) {
									if (NewList[i].msgId == e) {
										NewList.splice(i, 1)
									}
								}
								that.list = NewList
							})

							uni.showToast({
								icon: "success",
								title: '删除成功!',
								duration: 2000
							});
						} else if (res.cancel) {
							// console.log('用户点击取消');
							return
						}
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.msgTitle {
		font-size: 35rpx;
		font-weight: bold;
		color: black;
	}

	.time {
		margin-top: 5rpx;
		font-size: 20rpx;
		margin-bottom: 20rpx;
	}

	.msgContent {
		display: block;
		font-family: PingFangSC-Regular;
		color: #482929;
		letter-spacing: 0;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: normal;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
		/*规定最多显示四行*/
	}

	.remark {
		position: absolute;
		display: inline-block;
		bottom: 90rpx;
		font-family: PingFangSC-Regular;
		color: #482929;
	}

	.footerTop {
		position: absolute;
		display: inline-block;
		width: 620rpx;
		bottom: 10rpx;
		margin-top: 10rpx;
		font-size: 20rpx;
		font-weight: bold;
		line-height: 50rpx;
		border-top: 1rpx #dcdcdc solid;

		.information {
			font-size: 25rpx;
			vertical-align: top;
		}

		.iconfont {
			font-size: 30rpx;
			vertical-align: top;
			float: right;
		}
	}

	.bottomTime {
		text-align: center;
		font-size: 20rpx;
		color: #aaaaaa;

	}
</style>
